<?php
/**
 * Created by PhpStorm.
 * User: jason
 * Date: 2017/10/18
 * Time: 22:23
 */

namespace Home\Service;


use Think\Model;

class SheetFavoriteService extends Model
{
    /**
     * 获取用户喜欢的曲谱
     * @param $userId
     * @return bool
     */
    public function getUserFavList($userId)
    {
        if (empty($userId) || !is_numeric($userId)) {
            return false;
        }
        $map = array(
            'user_id' => $userId,
            'is_delete' => 0
        );
        $list = M('SheetFavorite')->field('id, sheet_id, user_id')->where($map)->select();
        foreach ($list as $val) {
            $sheetFavMap[$val['sheet_id']] = $val;
        }
        return $sheetFavMap;
    }

    /**
     * 喜欢/取消喜欢曲谱
     * @param $param
     * @return bool
     */
    public function favSheet($param)
    {
        $map = array(
            'user_id' => $param['user_id'],
            'sheet_id' => $param['sheet_id']
        );
        $res = M('SheetFavorite')->where($map)->find();
        if ($res) {
            if ($param['status'] == 0) {
                $param['is_delete'] = 1;
                unset($param['status']);
                $result = M('SheetFavorite')->where($map)->save($param);
            } else {
                $param['is_delete'] = 0;
                unset($param['status']);
                $result = M('SheetFavorite')->where($map)->save($param);
            }
        } else {
            $param['create_time'] = date('Y-m-d H:i:s', time());
            $result = M('SheetFavorite')->add($param);
        }
        if ($result) {
            return true;
        } else {
            return false;
        }
    }
}